package com.example.home_app.base.welcome

import android.os.Bundle
import android.view.View
import android.view.ViewGroup

import android.widget.RadioButton
import android.widget.RadioGroup

import androidx.viewpager.widget.ViewPager

import com.android.frame.mvp.CommonBaseActivity
import com.example.home_app.R
import com.example.home_app.adapter.SimpleViewAdapter
import com.example.home_app.base.MainActivity
import com.example.home_app.databinding.ActivityGuideBinding
import com.example.home_app.util.SPTool
import com.example.home_app.util.SizeUtil
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar


/**
 * Created by dell on 2021/8/11.
 */
class GuideActivity: CommonBaseActivity<ActivityGuideBinding>() {
    private var mLayoutIds: IntArray = intArrayOf(
        R.layout.layout_guide_one,
        R.layout.layout_guide_two,
        R.layout.layout_guide_three
    )

    override fun initBar() {
        //隐藏状态栏
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init()

    }

    override fun handleView(savedInstanceState: Bundle?) {
        val viewList: MutableList<View> = mutableListOf()
        for (i in mLayoutIds.indices) {
            val radioButton = RadioButton(this)
            radioButton.setButtonDrawable(R.drawable.selector_guide_indicator)
            val params = RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            val margin = SizeUtil.dp2pxInt(6f)
            params.leftMargin = margin
            params.rightMargin = margin
            radioButton.layoutParams = params
            radioButton.tag = i
            radioButton.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    binding.guideVp.setCurrentItem(buttonView.tag.toString().toInt(), false)
                }
            }
            binding.guideRg.addView(radioButton, params)
            if (i == 0) {
                radioButton.isChecked = true
            }
            val view = layoutInflater.inflate(mLayoutIds[i], null)
            viewList.add(view)
            binding.guideVp.adapter = SimpleViewAdapter(viewList)
            binding.guideVp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrollStateChanged(state: Int) {
                }

                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                }

                override fun onPageSelected(position: Int) {
                    val radioButton: RadioButton = binding.guideRg.findViewWithTag(position)
                    if (!radioButton.isChecked) {
                        radioButton.isChecked = true
                    }
                    binding.useBtn.visibility = if (position == mLayoutIds.size - 1) View.VISIBLE else View.INVISIBLE
                }

            })
        }
    }

    override fun initListener() {
        binding.useBtn.setOnClickListener {
            startActivity(MainActivity::class.java)
            finish()
        }
    }

}